package com.lsq.lwf.common.datasource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

import com.lsq.lwf.common.utils.PropertiesUtils;

public final class ConnectionDS {

	private static DataSource DS;

	private static ConnectionDS cds;

	// static{
	// init();
	// }

	public ConnectionDS() {
		init();
	}

	/**
	 * 单例模式 获取数据库连接辅助类
	 * 
	 * @return
	 */
	public synchronized static ConnectionDS getInstanse() {
		if (cds == null) {
			cds = new ConnectionDS();
		}
		return cds;
	}

	public static Connection getConnection() {

		Connection con = null;
		if (DS != null) {
			try {
				con = DS.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return con;
	}

	private static void init() {
		PropertiesUtils.fileName = "dbcpconfig.properties";
		String driverClassName = PropertiesUtils.getValue("driverClassName");
		String url = PropertiesUtils.getValue("url");
		String username = PropertiesUtils.getValue("username");
		String password = PropertiesUtils.getValue("password");
		int maxActive = Integer.parseInt(PropertiesUtils.getValue("maxActive"));
		int initialSize = Integer.parseInt(PropertiesUtils
				.getValue("initialSize"));
		long maxWait = Long.parseLong(PropertiesUtils.getValue("maxWait"));
		int maxIdle = Integer.parseInt(PropertiesUtils.getValue("maxIdle"));
		int minIdle = Integer.parseInt(PropertiesUtils.getValue("minIdle"));
		String validationQuery = PropertiesUtils.getValue("validationQuery");
		int removeAbandonedTimeout = Integer.parseInt(PropertiesUtils
				.getValue("removeAbandonedTimeout"));
		String encode = PropertiesUtils.getValue("connectionProperties");
		long timeBetweenEvictionRunsMillis = Long.parseLong(PropertiesUtils
				.getValue("timeBetweenEvictionRunsMillis"));

		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName(driverClassName);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setMaxActive(maxActive);
		ds.setInitialSize(initialSize);
		ds.setMaxWait(maxWait);
		ds.setMaxIdle(maxIdle);
		ds.setMinIdle(minIdle);
		ds.setTestOnBorrow(true);
		ds.setTestWhileIdle(true);
		ds.setValidationQuery(validationQuery);
		ds.setRemoveAbandoned(true);
		ds.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		ds.setConnectionProperties(encode);
		ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		DS = ds;
	}

	public static void  closeConnection(Connection con ){
		try {
			if(!con.isClosed()){
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void closeResultSet(ResultSet rs){
		try {
			if(!rs.isClosed()){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void closePreparedStatement(PreparedStatement pstmt){
		try {
			if(pstmt.isClosed()){
				pstmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ConnectionDS ds = new ConnectionDS();
		Connection c = ds.getConnection();
		System.out.println(c);
	}
}
